$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/buserpromotionsettleinfo/list',
        datatype: "json",
        colModel: [
			{ label: '用户ID', name: 'id', index: 'id',align: "center", width: 40, key: true,formatter:function(value,options,rowData){
					var userId=rowData["userId"];
					if(1==1){
						return "<a  onclick='update("+value+")'>"+userId+"</a>";
					}
				}
			},
			{ label: '一级比例', name: 'firstLevelProportion', index: 'first_level_proportion',align: "center", width: 45,formatter:function(value,options,rowData){
					return value+"%";
				}
			},
			{ label: '二级比例', name: 'secondLevelProportion', index: 'second_level_proportion',align: "center", width: 45,formatter:function(value,options,rowData){
					return value+"%";
				}
			},
            { label: '三级比例', name: 'thirdLevelProportion', index: 'third_level_proportion',align: "center", width: 45,formatter:function(value,options,rowData){
                return value+"%";
            	}
            },
			{ label: '四级比例', name: 'fourthLevelProportion', index: 'fourth_level_proportion',align: "center", width: 45,formatter:function(value,options,rowData){
					return value+"%";
				}
			},
			{ label: '五级比例', name: 'fifthLevelProportion', index: 'fifth_level_proportion',align: "center", width: 45,formatter:function(value,options,rowData){
					return value+"%";
				}
			},
			{ label: '一级收入', name: 'firstLevelCumulativeIncome', index: 'first_level_cumulative_income',align: "center", width: 45 },
			{ label: '二级收入', name: 'secondLevelCumulativeIncome', index: 'second_level_cumulative_income',align: "center", width: 45 },
            { label: '三级收入', name: 'thirdLevelCumulativeIncome', index: 'third_level_cumulative_income',align: "center", width: 45 },
			{ label: '四级收入', name: 'fourthLevelCumulativeIncome', index: 'fourth_level_cumulative_income',align: "center", width: 45 },
			{ label: '五级收入', name: 'fifthLevelCumulativeIncome', index: 'fifth_level_cumulative_income',align: "center", width: 45 },
			{ label: '累计抵扣佣金', name: 'allBrokerage', index: 'all_brokerage',align: "center", width: 65 },

			{ label: '累计收入', name: 'cumulativeIncome', index: 'cumulative_income',align: "center", width: 45 },
			{ label: '已结算', name: 'settledIncome', index: 'settled_income',align: "center", width: 40 },
			{ label: '渠道描述', name: 'settleDesc', index: 'settle_desc',align: "center", width: 70 },
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 88 },
			{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 88 },
			{ label: '操作', name: '', index: '',align: "center", width: 80 ,formatter:function(value,options,rowData){
					var cumulativeIncome=rowData["cumulativeIncome"];
					var settledIncome=rowData["settledIncome"];
					var userId=rowData["userId"];
					if(cumulativeIncome!=settledIncome){
						return "<button class='btn btn-primary'  onclick=vm.dakuan("+userId+")>"+'打款'+"</button>"+"&nbsp;"+
						 "<button class='btn btn-primary'  onclick=vm.tuiguang("+userId+")>"+'生成'+"</button>";
					}else{
						return "<button class='btn btn-primary' disabled='disabled' onclick=vm.dakuan("+userId+")>"+'打款'+"&nbsp;&nbsp;"+
							"<button class='btn btn-primary'  onclick=vm.tuiguang("+userId+")>"+'生成'+"</button>";
					}
				}
			},
		],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true,
        rownumWidth: 35,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
		postData:{"userId":vm.q.userId},
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        	$("#jqgh_jqGrid_cb").css("text-align", "left");
        }
    });
});
function update(e) {
	vm.showList = false;
	vm.title = "修改";
	console.log(e);
	vm.getInfo(e);
	// 显示 Tab 标签页
	$("ul.nav-pills").find("li").each(function () {
		$(this).show();
	})
	// 修改状态下，用户编号设为可读
	$("#current_userId").attr("readOnly", "readOnly");
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		addPay:true,
		tuiguan:true,
		imgdow:true,
		title: null,
		bUserPromotionSettleInfo: {},
		bUserPromotionPaymentInfo:{
			userId:"",
			paymentAmount:""
		},
		q:{
			userId:""
		},
	},
	methods: {
		dakuan:function(e){
			$("#userId").attr("value",e);
			vm.bUserPromotionPaymentInfo.userId=e;
			vm.bUserPromotionPaymentInfo.paymentAmount="";
			layer.open({
				type: 1,
				title: '新增打款记录',
				maxmin: true,
				shadeClose: true, //点击遮罩关闭层
				area : ['600px' , '280px'],
				content: $("#addPay").html()
			});

		},
		closeAll:function(){
			layer.closeAll();
		},
		getValue:function(e){
			vm.bUserPromotionPaymentInfo.paymentAmount=e.value;
		},
		payAdd:function(){
			$.ajax({
				type: "POST",
				url: baseURL + "sys/buserpromotionpaymentinfo/save",
				contentType: "application/json",
				data: JSON.stringify(vm.bUserPromotionPaymentInfo),
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							layer.closeAll();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		tuiguang:function(e){
			$.ajax({
				type: "POST",
				url: baseURL + "sys/buserpromotionsettleinfo/tuiguang/"+e,
				contentType: "application/json",
				success: function(r){
					console.log(r);
					if(r.code==0){
						var session=r.sessionId;
						$.ajax({
							type: "POST",
							url: "https://vtool.xiafanapp.cn//generateUserPromotionQrCode",
							headers:{"userId":e,"userSessionId":session},
							contentType: "application/json",
							success: function (r) {
								if(r.head.code==200) {
									$("#wechat").attr("src", r.data.userLongTermQrCodeImgUrl);
									layer.open({
										type: 1,
										title: '推广码',
										maxmin: true,
										shadeClose: true, //点击遮罩关闭层
										area: ['400px', '280px'],
										content: $("#imgdow").html()
									});
								}else{
									alert(r.head.msg);
								}
							}
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.bUserPromotionSettleInfo = {};
			// 隐藏 Tab 标签页
			$("ul.nav-pills").find("li").each(function () {
				$(this).css("display", "none");
			})
			// 新增状态下，用户编号解除可读
			$("#current_userId").removeAttr("readonly");
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.bUserPromotionSettleInfo.id == null ? "sys/buserpromotionsettleinfo/save" : "sys/buserpromotionsettleinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.bUserPromotionSettleInfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/buserpromotionsettleinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/buserpromotionsettleinfo/info/"+id, function(r){
                vm.bUserPromotionSettleInfo = r.bUserPromotionSettleInfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                page:page,
				postData:{"userId":vm.q.userId},
            }).trigger("reloadGrid");
		}
	}
});
